/**
 
 $Id: tServ.h 2 2012-04-04 15:07:32Z gerhardus1@gmail.com $
 Versioning: a.b.c a is a major release, b represents changes or new features, c represents bug fixes. 
 @version 1.0.0   06/10/2011    Gerhardus Muller     Script created

 @note

 @todo
 
 @bug

 Copyright Gerhardus Muller
 */

#if !defined( tServ_defined_ )
#define tServ_defined_

#include "../src/options.h"
#include "message/tMessage.h"

class epoll;
class recoveryLog;

class tServ : public object
{
  // Definitions
  public:

    // Methods
  public:
    tServ( int theArgc, char* theArgv[] );
    virtual ~tServ();
    virtual std::string toString( );
    void main( );
    static void sigHandler( int signo );
    void runAsDaemon( );
    void cleanupObj( );

  private:
    void init( );
    bool handleChildDone( );
    void waitForChildrenToExit( );
    bool dropPriviledge( const char* user );
    void termChildren( );
//    void signalChildren( int sig );
//    void sendCommandToChild( tMessage::eCommandType command, int fd );
//    void sendCommandToChildren( tMessage::eCommandType command );
//    void sendCommandToChildren( tMessage* pCommand );
    static void sendSignalCommand( tMessage::eCommandType command );
    void reconfigure( tMessage* pCommand );

    // Properties
  public:
    static bool                       bRunning;                   ///< true to run, false to exit
    static bool                       bChildSignal;               ///< true if a SIGCHLD was caught
    static int                        signalFd0;                  ///< signalFd[0] for the signal handling

  protected:

  private:
    unixSocket*                       pRecSock;                   ///< socket for accepting incoming events
    unixSocket*                       pSignalSock;                ///< socket for received signal events
    epoll*                            pEpollRd;                   ///< fd poll object for reading
    epoll*                            pEpollWr;                   ///< fd poll object for writing
    int                               pid;                        ///< own pid
    int                               signalFd[2];                ///< unix domain socket to submit signal events to the tServ parent process - tServ listens on [1] - now using the external socket
    std::string                       hostId;                     ///< hostname entry
    int                               argc;                       ///< command line parameters
    char**                            argv;                       ///< command line parameters
    unsigned int                      now;                        ///< time at the beginning of the loop
    unsigned int                      nextExpiredEventCheck;      ///< next time to check for expired event
};	// class tServ

void cleanup( );
extern const char* buildno;
extern const char* buildtime;
#endif // !defined( tServ_defined_)

